import {createRouter, createWebHistory} from 'vue-router'
import {useStore} from "vuex";

const routerHistory = createWebHistory()

const router = createRouter({
    history: routerHistory,
    routes: [
        {
            path: '/',
            name: 'Home',
            component: () => import('@/view/Index/index.vue')
        },
        {
            path: '/login',
            name: 'Login',
            component: () => import('@/view/login.vue')
        },
        {
            path: '/fishFever',
            name: 'fishFever',
            meta:{
                title: "浑水摸鱼"
            },
            component: () => import('@/view/fishFever/index.vue')
        },
        {
            path: '/bindQQCallbacks',
            name: 'bindQQCallbacks',
            component: () => import('@/view/bindQQCallbacks.vue')
        },
        {
            path: '/bindQQUpdate',
            name: 'bindQQUpdate',
            meta: {
                isLogin: true
            },
            component: () => import('@/view/bindQQUpdate.vue')
        },
        {
            path: '/user',
            name: 'User',
            meta: {
                isLogin: true
            },
            redirect: "userHome",
            component: () => import('@/view/User/index.vue'),
            children: [
                {
                    path: '/user',
                    name: 'userHome',
                    component: () => import('@/view/User/userHome.vue')
                },
                {
                    path: '/user/keep',
                    name: 'keep',
                    component: () => import('@/view/User/keep.vue')
                },
                {
                    path: '/user/article',
                    name: 'userArticle',
                    component: () => import('@/view/User/article.vue')
                },
                {
                    path: '/user/comment',
                    name: 'userComment',
                    component: () => import('@/view/User/comment.vue')
                },
                {
                    path: '/user/report',
                    name: 'userReport',
                    component: () => import('@/view/User/report.vue')
                },
                {
                    // 加载页面
                    path: '/user/loading',
                    name: 'userLoading',
                    component: () => import('@/view/User/loading.vue')
                },
            ]
        },
        {
            path: '/article',
            name: 'Article',
            component: () => import('@/view/Article/index.vue'),
            redirect: "ArticleIndex",
            children: [
                {
                    path: '/article',
                    name: 'ArticleIndex',
                    component: () => import('@/view/Article/article.vue')
                },
                {
                    path: '/detail/:id',
                    name: 'ArticleDetail',
                    component: () => import('@/view/Article/detail.vue')
                }
            ]
        },
        {
            path: '/message',
            name: 'Message',
            component: () => import('@/view/Message/index.vue'),
            redirect: "MessageIndex",
            meta: {
                isLogin: true
            },
            children: [
                {
                    path: '/message',
                    name: 'MessageIndex',
                    component: () => import('@/view/Message/message.vue')
                }
            ]
        },
    ]
})

router.beforeEach((to:any) => {
    const store = useStore();
    store.dispatch('InitLoginStatus');  // 每次跳转页面前初始化用户数据获取
    console.log(to,"to")
    if(to.meta?.title){
        document.title = to.meta.title;
    }
    // 跳转后让页面回到底部
    window.scrollTo(0, 0);
    if (
        // 检查用户是否已登录
        !store.getters.loginStatus &&
        // 是否需要验证
        to.meta.isLogin
    ) {
        // 将用户重定向到登录页面
        return {name: 'Login'}
    }
})

router.afterEach(() => {
    const store = useStore();
    console.log(store.state.Token, "store.state")
    if (store.getters.loginStatus && !store.state.websocket) {
        store.commit('createWebSocket', import.meta.env.VITE_WEBSOCKET_URL)
    }
})

export default router;
